//
//  ht_util.m
//  ht1881
//
//  Created by george on 06/03/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "apps123_util.h"


@implementation apps123_util

+ (BOOL) isEmptyString:(NSString *)str{
	if (str==nil || str.length<=0 || [str isEqualToString:@""] ) return YES;
	return FALSE;
}

+ (void)showDialog:(NSString *)mTitle Message:(NSString *)mess{
	//NSLog(@"show dialog");
	//UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:mTitle message:mess delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
   	//[alertView show];
	//alertView=nil;
	
}

+ (void)showYesNoDialog:(NSString *)mTitle Message:(NSString *)mess YesSelector:(SEL)yesSelector NoSelector:(SEL)noSelector{
    //UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:mTitle message:mess delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
	//[alertView show];
}

+ (NSString*)removeJsonCallback:(NSString *)str{
    if ( [apps123_util isEmptyString:str] || str.length<17 ) return nil;
    
	NSString *pattern=@"apps123callback(";
    NSString *newStr=[str stringByReplacingOccurrencesOfString:pattern withString:@"" options:NSAnchoredSearch range:NSMakeRange(0,17)];
   // NSLog(@"%@",newStr);
  //  NSLog(@"length of newStr:%d",newStr.length);
    NSString *newStr2=[newStr stringByReplacingOccurrencesOfString:@")" withString:@"" options:NSBackwardsSearch|NSAnchoredSearch range:NSMakeRange(0,newStr.length)];
  //  NSLog(@"newStr:%@",newStr2);
    return newStr2;
    
}

+ (NSString *)getResourceFromBundle:(NSString *)folder name:(NSString *)name type:(NSString *)type {
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *bundlePath = [bundle pathForResource:@"projectinfo" ofType:@"bundle"];
    NSBundle *projectBundle = [NSBundle bundleWithPath:bundlePath];
    NSString *resourcePath = [projectBundle pathForResource:[NSString stringWithFormat:@"www/%@/%@",folder,name] ofType:type];
    return resourcePath;
}

+ (NSString *)abstractLastFix:(NSString *)string {
    NSArray *strings = [string componentsSeparatedByString:@"/"];
    if([strings count] > 0){
        NSString *lastFix = [strings objectAtIndex:[strings count] - 1];
        //去掉?后面的字符串
        NSRange range = [lastFix rangeOfString:@"?"];
        if(range.length != 0){
            lastFix = [lastFix substringToIndex:range.location];
        }
        return lastFix;
    }
    return @"";
}

+ (NSString *)append:(NSString *)string withLastFix:(NSString *)lastFix {
    NSString *pathWithOutExt = [string stringByDeletingPathExtension];
    NSString *ext = [string pathExtension];
    
    return [NSString stringWithFormat:@"%@c.%@",pathWithOutExt,ext];
}

+ (NSString *)generateControllerClassName:(NSString *)sysTabName layout:(NSString *)layout {
    if(sysTabName && layout && ![sysTabName isEqualToString:@""] && ![layout isEqualToString:@""]){
        sysTabName = [sysTabName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
        NSString *firstCharacter = [layout substringToIndex:1];
        NSString *lastCharacters = [layout substringFromIndex:1];
        layout = [NSString stringWithFormat:@"%@%@",[firstCharacter uppercaseString],lastCharacters];
        
        return [NSString stringWithFormat:@"%@%@ViewController",sysTabName,layout];
    }
    return @"";
}

+ (NSString *)generateLayoutControllerClassName:(NSString *)sysTabName layout:(NSString *)layout detailLayout:(NSString *)detailLayout {
    if(sysTabName && layout && ![sysTabName isEqualToString:@""] && ![layout isEqualToString:@""] && ![detailLayout isEqualToString:@""]){
        detailLayout = [detailLayout stringByReplacingOccurrencesOfString:@".html" withString:@""];
        sysTabName = [sysTabName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
        NSString *firstCharacter = [layout substringToIndex:1];
        NSString *lastCharacters = [layout substringFromIndex:1];
        layout = [NSString stringWithFormat:@"%@%@",[firstCharacter uppercaseString],lastCharacters];
        
        return [NSString stringWithFormat:@"%@%@DetailLayout%@ViewController",sysTabName,layout,detailLayout];
    }
    return @"";
}

+ (NSString *)generateLayoutControllerClassName:(NSString *)sysTabName layout:(NSString *)layout levelLayout:(NSString *)levelLayout {
    if(sysTabName && layout && ![sysTabName isEqualToString:@""] && ![layout isEqualToString:@""] && ![levelLayout isEqualToString:@""]){
        levelLayout = [levelLayout stringByReplacingOccurrencesOfString:@".html" withString:@""];
        sysTabName = [sysTabName stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
        NSString *firstCharacter = [layout substringToIndex:1];
        NSString *lastCharacters = [layout substringFromIndex:1];
        layout = [NSString stringWithFormat:@"%@%@",[firstCharacter uppercaseString],lastCharacters];
        
        return [NSString stringWithFormat:@"%@%@LevelLayout%@ViewController",sysTabName,layout,levelLayout];
    }
    return @"";
}

+ (NSString *)filterRequestServer:(NSString *)serverUrl {
    serverUrl = [serverUrl stringByReplacingOccurrencesOfString:@"http://" withString:@""];
    return [NSString stringWithFormat:@"%@/Apps123",serverUrl];
}

+ (NSString *)filterTimeLength:(NSString *)time {
    return time;
}

+ (NSString *)currentSystemVersion {
    //CGFloat versionNumber = 0.f;
    //NSString *version = [UIDevice currentDevice].systemVersion;
    /*
    if(version){
        versionNumber = [version floatValue];
    }*/
    return 0;//[NSString stringWithFormat:@"%.1f",versionNumber];
}

+ (NSInteger)getStringLength:(NSString *)string {
    int strlength = 0;
    char *p = (char*)[string cStringUsingEncoding:NSUnicodeStringEncoding];
    for(int i = 0 ; i < [string lengthOfBytesUsingEncoding:NSUnicodeStringEncoding];i++) {
        if(*p) {
            p++;
            strlength++;
        }else{
            p++;
        }
        
    }
    return strlength;
//    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
//    NSData *da = [string dataUsingEncoding:enc];
//    return [da length];
}

+ (NSString *)htmlEntityDecode:(NSString *)string {
    string = [string stringByReplacingOccurrencesOfString:@"&quot;" withString:@"\""];
    string = [string stringByReplacingOccurrencesOfString:@"&apos;" withString:@"'"];
    string = [string stringByReplacingOccurrencesOfString:@"&amp;" withString:@"&"];
    string = [string stringByReplacingOccurrencesOfString:@"&lt;" withString:@"<"];
    string = [string stringByReplacingOccurrencesOfString:@"&gt;" withString:@">"];
    return string;
}
/*
+ (void)alignLabelWithTop:(UILabel *)label maxHeight:(CGFloat)maxHeight {
    CGSize maxSize = CGSizeMake(label.frame.size.width, maxHeight);
    label.adjustsFontSizeToFitWidth = NO;
    CGSize actualSize = [label.text sizeWithFont:label.font constrainedToSize:maxSize lineBreakMode:label.lineBreakMode];
    CGRect rect = label.frame;
    rect.size.height = actualSize.height;
    label.frame = rect;
}
*/
@end
